package com.health.system.security.handle;

import com.health.system.common.result.Result;
import com.health.system.common.utils.JsonUtil;
import org.springframework.http.HttpStatus;
import org.springframework.security.access.AccessDeniedException;
import org.springframework.security.web.access.AccessDeniedHandler;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * 权限拒绝访问处理器
 *
 */
public class BlogAccessDeniedHandler implements AccessDeniedHandler {
    @Override
    public void handle(HttpServletRequest request, HttpServletResponse response, AccessDeniedException accessDeniedException) throws IOException {
        response.setStatus(HttpStatus.INTERNAL_SERVER_ERROR.value());
        response.setContentType("application/json;charset=UTF-8");

        String result = JsonUtil.writer(Result.fail("权限拒绝, 原因为" + accessDeniedException.getMessage()));
        response.getWriter().write(result);
    }
}
